package com.hyt.it.ogt.controller.pj;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.pj.common.Constants;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.model.entity.Task;
import com.hyt.it.ogt.pj.model.vo.MarkData;
import com.hyt.it.ogt.pj.service.ITaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;

/**
 * <p>
 * 评卷任务表 前端控制器
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@ApiSort(value = 12)
@Api(tags = "12.评卷任务接口", value = "评卷任务接口")
@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {

    @Autowired
    private ITaskService taskService;

    @ApiOperationSort(value = 1)
    @ApiOperation(value = "12.1.1根据id查询任务", notes = Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("query/{id}")
    public Object query(@PathVariable("id") String id){
        Task task = taskService.getById(id);
        if(task != null){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(task).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    /**
     * 获取评卷任务
     * @param projectId 评卷项目id
     * @param subjectId 评卷科目id
     * @param pieceCode 待用，整卷评卷模式下不存在
     * @return
     */
    @ApiOperationSort(value = 2)
    @ApiOperation(value = "12.1.2获取评卷任务", notes = Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("findTask")
    public Object findTask(@RequestParam("projectId") String projectId, String subjectId, String pieceCode){
        String userId = getUserId();
        List<MarkData> markDataList = taskService.findTask(projectId, subjectId, pieceCode, userId);
        if(markDataList != null){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(markDataList).build();
        }else{
            // 若获取评卷数据为空，再次调用获取评卷任务，本次进入后会切换科目
        	markDataList = taskService.findTask(projectId, subjectId, pieceCode, userId);
            // 若切换科目后再次获取任务再次失败，说明所有科目都已经评完，没有任务了
            if(markDataList == null){
                return ApiResponse.builder().code(ResponseCode.SUCCESS).data(ResponseCode.FIND_MARK_TASK_NULL).build();
            }else {
                return ApiResponse.builder().code(ResponseCode.SUCCESS).data(markDataList).build();
            }
        }
    }

    @ApiOperationSort(value = 3)
    @ApiOperation(value = "12.1.3获取指定考生任务", notes = "")
    @GetMapping("findStudentTask")
    public Object findStudentTask(@RequestParam("projectId")String projectId, @RequestParam("subjectId")String subjectId, @RequestParam("admissionNum")String admissionNum){
        String userId = getUserId();
        List<MarkData> markDataList = taskService.findStudentTask(projectId, subjectId, admissionNum, userId);
        if(markDataList != null){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(markDataList).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(ResponseCode.FIND_MARK_TASK_NULL).build();
        }
    }

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "12.2.1添加任务", notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("add")
    public Object add(@RequestBody Task task){
        boolean flag = taskService.save(task);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "12.3.1更新任务", notes = Constants.SWAGGER_METHOD_NOTES)
    @PutMapping("update")
    public Object update(@RequestBody Task task){
        boolean flag = taskService.updateById(task);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 21)
    @ApiOperation(value = "12.3.2提交任务", notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("submitTask")
    public Object submitTask(@RequestBody MarkData markData){
        String userId = getUserId();
        Integer code = taskService.submitTask(markData, userId);
        return ApiResponse.builder().code(code).build();
    }

    @ApiOperationSort(value = 31)
    @ApiOperation(value = "12.4.1根据id删除任务", notes = Constants.SWAGGER_METHOD_NOTES)
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") String id){
        boolean flag = taskService.removeById(id);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

}
